package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreProductRuleBo;
import com.ruoyi.system.domain.vo.PStoreProductRuleVo;
import com.ruoyi.system.domain.PStoreProductRule;
import com.ruoyi.system.mapper.PStoreProductRuleMapper;
import com.ruoyi.system.service.IPStoreProductRuleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品规则值(规格)Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@RequiredArgsConstructor
@Service
public class PStoreProductRuleServiceImpl implements IPStoreProductRuleService {

    private final PStoreProductRuleMapper baseMapper;

    /**
     * 查询商品规则值(规格)
     */
    @Override
    public PStoreProductRuleVo queryById(Integer id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品规则值(规格)列表
     */
    @Override
    public TableDataInfo<PStoreProductRuleVo> queryPageList(PStoreProductRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreProductRule> lqw = buildQueryWrapper(bo);
        Page<PStoreProductRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品规则值(规格)列表
     */
    @Override
    public List<PStoreProductRuleVo> queryList(PStoreProductRuleBo bo) {
        LambdaQueryWrapper<PStoreProductRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreProductRule> buildQueryWrapper(PStoreProductRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreProductRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotEmpty(bo.getId()), PStoreProductRule::getId, bo.getId());
        lqw.like(StringUtils.isNotBlank(bo.getRuleName()), PStoreProductRule::getRuleName, bo.getRuleName());
        lqw.eq(StringUtils.isNotBlank(bo.getRuleValue()), PStoreProductRule::getRuleValue, bo.getRuleValue());
        return lqw;
    }

    /**
     * 新增商品规则值(规格)
     */
    @Override
    public Boolean insertByBo(PStoreProductRuleBo bo) {
        PStoreProductRule add = BeanUtil.toBean(bo, PStoreProductRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品规则值(规格)
     */
    @Override
    public Boolean updateByBo(PStoreProductRuleBo bo) {
        PStoreProductRule update = BeanUtil.toBean(bo, PStoreProductRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreProductRule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品规则值(规格)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
